using System;

namespace DnsService.Models
{
    /// <summary>
    /// Socket客户端信息
    /// 作者：huobing
    /// </summary>
    public class SocketClientInfo
    {
        /// <summary>
        /// 客户端唯一标识
        /// </summary>
        public string? ClientId { get; set; }
        
        /// <summary>
        /// 客户端IP地址
        /// </summary>
        public string? IPAddress { get; set; }
        
        /// <summary>
        /// 连接时间
        /// </summary>
        public DateTime ConnectedTime { get; set; }
        
        /// <summary>
        /// 最后活动时间
        /// </summary>
        public DateTime LastActivityTime { get; set; }
        
        /// <summary>
        /// 接收的消息数量
        /// </summary>
        public int MessagesReceived { get; set; }
        
        /// <summary>
        /// 发送的消息数量
        /// </summary>
        public int MessagesSent { get; set; }
        
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnline { get; set; }
        
        /// <summary>
        /// 连接时长（秒）
        /// </summary>
        public double ConnectionDurationSeconds => IsOnline ? (DateTime.Now - ConnectedTime).TotalSeconds : 0;
        
        /// <summary>
        /// 空闲时长（秒）
        /// </summary>
        public double IdleDurationSeconds => (DateTime.Now - LastActivityTime).TotalSeconds;
    }

    /// <summary>
    /// Socket连接统计信息
    /// 作者：huobing
    /// </summary>
    public class SocketConnectionStatistics
    {
        /// <summary>
        /// 总连接数
        /// </summary>
        public int TotalConnections { get; set; }
        
        /// <summary>
        /// 当前连接数
        /// </summary>
        public int CurrentConnections { get; set; }
        
        /// <summary>
        /// 接收的消息总数
        /// </summary>
        public int TotalMessagesReceived { get; set; }
        
        /// <summary>
        /// 发送的消息总数
        /// </summary>
        public int TotalMessagesSent { get; set; }
        
        /// <summary>
        /// 服务器启动时间
        /// </summary>
        public DateTime ServerStartTime { get; set; }
        
        /// <summary>
        /// 服务器运行时长（秒）
        /// </summary>
        public double ServerUptimeSeconds => (DateTime.Now - ServerStartTime).TotalSeconds;
        
        /// <summary>
        /// 平均消息处理时间（毫秒）
        /// </summary>
        public double AverageMessageProcessingTimeMs { get; set; }
        
        /// <summary>
        /// 消息处理成功率
        /// </summary>
        public double MessageProcessingSuccessRate { get; set; }
        
        /// <summary>
        /// 连接成功率
        /// </summary>
        public double ConnectionSuccessRate { get; set; }
    }

    /// <summary>
    /// Socket服务器状态信息
    /// 作者：huobing
    /// </summary>
    public class SocketServerStatus
    {
        /// <summary>
        /// 服务器是否正在运行
        /// </summary>
        public bool IsRunning { get; set; }
        
        /// <summary>
        /// 监听端口
        /// </summary>
        public int Port { get; set; }
        
        /// <summary>
        /// 当前连接数
        /// </summary>
        public int CurrentConnections { get; set; }
        
        /// <summary>
        /// 最大连接数
        /// </summary>
        public int MaxConnections { get; set; }
        
        /// <summary>
        /// 服务器启动时间
        /// </summary>
        public DateTime? StartTime { get; set; }
        
        /// <summary>
        /// 服务器运行时长（秒）
        /// </summary>
        public double UptimeSeconds => StartTime.HasValue ? (DateTime.Now - StartTime.Value).TotalSeconds : 0;
        
        /// <summary>
        /// 性能指标
        /// </summary>
        public SocketPerformanceMetrics? PerformanceMetrics { get; set; }
        
        /// <summary>
        /// 服务器健康状态
        /// </summary>
        public string HealthStatus => CalculateHealthStatus();
        
        /// <summary>
        /// 计算服务器健康状态
        /// </summary>
        /// <returns>健康状态字符串</returns>
        private string CalculateHealthStatus()
        {
            if (!IsRunning) return "Stopped";
            
            var metrics = PerformanceMetrics;
            if (metrics == null) return "Unknown";
            
            if (metrics.MemoryUsageMB > 500) return "Warning";
            if (metrics.MessageProcessingSuccessRate < 0.9) return "Degraded";
            if (CurrentConnections >= MaxConnections * 0.8) return "HighLoad";
            
            return "Healthy";
        }
    }

    /// <summary>
    /// Socket性能指标
    /// 作者：huobing
    /// </summary>
    public class SocketPerformanceMetrics
    {
        /// <summary>
        /// 平均消息处理时间（毫秒）
        /// </summary>
        public double AverageMessageProcessingTimeMs { get; set; }
        
        /// <summary>
        /// 消息处理成功率
        /// </summary>
        public double MessageProcessingSuccessRate { get; set; }
        
        /// <summary>
        /// 连接成功率
        /// </summary>
        public double ConnectionSuccessRate { get; set; }
        
        /// <summary>
        /// 内存使用量（MB）
        /// </summary>
        public double MemoryUsageMB { get; set; }
        
        /// <summary>
        /// CPU使用率（百分比）
        /// </summary>
        public double CpuUsagePercent { get; set; }
        
        /// <summary>
        /// 网络吞吐量（KB/s）
        /// </summary>
        public double NetworkThroughputKbps { get; set; }
    }

    /// <summary>
    /// Socket配置选项
    /// 作者：huobing
    /// </summary>
    public class SocketConfiguration
    {
        /// <summary>
        /// 监听端口
        /// </summary>
        public int Port { get; set; } = 8080;
        
        /// <summary>
        /// 最大连接数
        /// </summary>
        public int MaxConnections { get; set; } = 1000;
        
        /// <summary>
        /// 接收缓冲区大小（字节）
        /// </summary>
        public int ReceiveBufferSize { get; set; } = 4096;
        
        /// <summary>
        /// 发送缓冲区大小（字节）
        /// </summary>
        public int SendBufferSize { get; set; } = 4096;
        
        /// <summary>
        /// 连接超时时间（秒）
        /// </summary>
        public int ConnectionTimeoutSeconds { get; set; } = 30;
        
        /// <summary>
        /// 心跳间隔（秒）
        /// </summary>
        public int HeartbeatIntervalSeconds { get; set; } = 60;
        
        /// <summary>
        /// 是否启用心跳检测
        /// </summary>
        public bool EnableHeartbeat { get; set; } = true;
        
        /// <summary>
        /// 是否启用SSL/TLS
        /// </summary>
        public bool EnableSsl { get; set; } = false;
        
        /// <summary>
        /// SSL证书路径
        /// </summary>
        public string? SslCertificatePath { get; set; }
        
        /// <summary>
        /// SSL证书密码
        /// </summary>
        public string? SslCertificatePassword { get; set; }
    }

    /// <summary>
    /// Socket消息封装
    /// 作者：huobing
    /// </summary>
    public class SocketMessage
    {
        /// <summary>
        /// 消息ID
        /// </summary>
        public string? MessageId { get; set; }
        
        /// <summary>
        /// 消息类型
        /// </summary>
        public string? MessageType { get; set; }
        
        /// <summary>
        /// 消息内容
        /// </summary>
        public string? Content { get; set; }
        
        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; }
        
        /// <summary>
        /// 发送者ID
        /// </summary>
        public string? SenderId { get; set; }
        
        /// <summary>
        /// 接收者ID
        /// </summary>
        public string? ReceiverId { get; set; }
        
        /// <summary>
        /// 消息优先级
        /// </summary>
        public int Priority { get; set; } = 1;
        
        /// <summary>
        /// 是否需要确认
        /// </summary>
        public bool RequiresAck { get; set; } = false;
        
        /// <summary>
        /// 消息元数据
        /// </summary>
        public Dictionary<string, object>? Metadata { get; set; }
    }

    /// <summary>
    /// Socket操作结果
    /// 作者：huobing
    /// </summary>
    public class SocketOperationResult
    {
        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Success { get; set; }
        
        /// <summary>
        /// 操作消息
        /// </summary>
        public string? Message { get; set; }
        
        /// <summary>
        /// 错误代码
        /// </summary>
        public string? ErrorCode { get; set; }
        
        /// <summary>
        /// 异常信息
        /// </summary>
        public Exception? Exception { get; set; }
        
        /// <summary>
        /// 操作数据
        /// </summary>
        public object? Data { get; set; }
        
        /// <summary>
        /// 操作时间戳
        /// </summary>
        public DateTime Timestamp { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SocketOperationResult()
        {
            Timestamp = DateTime.Now;
        }

        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="message">成功消息</param>
        /// <param name="data">操作数据</param>
        /// <returns>成功结果</returns>
        public static SocketOperationResult SuccessResult(string? message = null, object? data = null)
        {
            return new SocketOperationResult
            {
                Success = true,
                Message = message ?? "操作成功",
                Data = data
            };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="exception">异常信息</param>
        /// <returns>失败结果</returns>
        public static SocketOperationResult FailureResult(string? message = null, string? errorCode = null, Exception? exception = null)
        {
            return new SocketOperationResult
            {
                Success = false,
                Message = message ?? "操作失败",
                ErrorCode = errorCode,
                Exception = exception
            };
        }
    }
}